"""Buildfarm build and test"""

module(
    name = "build_buildfarm",
    repo_name = "build_buildfarm",
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "blake3", version = "1.5.1")
bazel_dep(name = "buildifier_prebuilt", version = "6.4.0")
bazel_dep(name = "gazelle", version = "0.35.0", repo_name = "bazel_gazelle")
bazel_dep(name = "grpc-java", version = "1.64.0")
bazel_dep(name = "googleapis", version = "0.0.0-20240326-1c8d509c5", repo_name = "com_google_googleapis")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "protobuf", version = "23.1", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_go", version = "0.46.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_java", version = "7.4.0")
bazel_dep(name = "rules_jvm_external", version = "6.0")
bazel_dep(name = "rules_license", version = "0.0.7")
bazel_dep(name = "rules_oci", version = "1.7.4")
bazel_dep(name = "rules_pkg", version = "0.10.1")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2")

# Test dependencies
bazel_dep(
    name = "container_structure_test",
    version = "1.16.0",
    dev_dependency = True,
)

# TODO: remove this after https://github.com/bazelbuild/remote-apis/pull/293 is merged
bazel_dep(name = "remoteapis", version = "eb433accc6a666b782ea4b787eb598e5c3d27c93")
archive_override(
    module_name = "remoteapis",
    integrity = "sha256-68wzxNAkPZ49/zFwPYQ5z9MYbgxoeIEazKJ24+4YqIQ=",
    strip_prefix = "remote-apis-eb433accc6a666b782ea4b787eb598e5c3d27c93",
    urls = [
        "https://github.com/bazelbuild/remote-apis/archive/eb433accc6a666b782ea4b787eb598e5c3d27c93.zip",
    ],
)

IO_NETTY_MODULES = [
    # keep sorted
    "buffer",
    "codec",
    "codec-http",
    "codec-http2",
    "codec-socks",
    "common",
    "handler",
    "handler-proxy",
    "resolver",
    "transport",
    "transport-classes-epoll",
    "transport-classes-kqueue",
    "transport-native-epoll",
    "transport-native-kqueue",
    "transport-native-unix-common",
]

IO_GRPC_MODULES = [
    # keep sorted
    "api",
    "auth",
    "context",
    "core",
    "netty",
    "netty-shaded",
    "protobuf",
    "services",
    "stub",
    "testing",
]

COM_AWS_MODULES = [
    # keep sorted
    "core",
    "s3",
    "secretsmanager",
]

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
    artifacts = ["com.amazonaws:aws-java-sdk-%s:1.12.544" % module for module in COM_AWS_MODULES] + [
                    # keep sorted
                    "com.fasterxml.jackson.core:jackson-databind:2.15.0",
                    "com.github.ben-manes.caffeine:caffeine:2.9.0",
                    "com.github.docker-java:docker-java:3.3.3",
                    "com.github.docker-java:docker-java-api:3.3.3",
                    "com.github.docker-java:docker-java-core:3.3.3",
                    "com.github.fppt:jedis-mock:1.0.13",
                    "com.github.jnr:jffi:1.3.11",
                    "com.github.jnr:jffi:jar:native:1.3.11",
                    "com.github.jnr:jnr-constants:0.10.4",
                    "com.github.jnr:jnr-ffi:2.2.14",
                    "com.github.jnr:jnr-posix:3.1.17",
                    "com.github.luben:zstd-jni:1.5.5-7",
                    "com.github.oshi:oshi-core:6.4.5",
                    "com.github.pcj:google-options:1.0.0",
                    "com.github.serceman:jnr-fuse:0.5.7",
                    "com.google.api.grpc:proto-google-common-protos:2.29.0",
                    "com.google.auth:google-auth-library-credentials:1.19.0",
                    "com.google.auth:google-auth-library-oauth2-http:1.19.0",
                    "com.google.code.findbugs:jsr305:3.0.2",
                    "com.google.code.gson:gson:2.10.1",
                    "com.google.errorprone:error_prone_annotations:2.27.0",
                    "com.google.errorprone:error_prone_core:2.27.0",
                    "com.google.guava:failureaccess:1.0.2",
                    "com.google.guava:guava:33.1.0-jre",
                    "com.google.j2objc:j2objc-annotations:2.8",
                    "com.google.jimfs:jimfs:1.3.0",
                    "com.google.protobuf:protobuf-java:3.19.1",
                    "com.google.protobuf:protobuf-java-util:3.19.1",
                    "com.google.truth:truth:1.4.2",
                    "com.googlecode.json-simple:json-simple:1.1.1",
                    "commons-io:commons-io:2.15.1",
                ] + ["io.netty:netty-%s:4.1.108.Final" % module for module in IO_NETTY_MODULES] +
                ["io.grpc:grpc-%s:1.62.2" % module for module in IO_GRPC_MODULES] + [
        # keep sorted
        "io.prometheus:simpleclient:0.16.0",
        "io.prometheus:simpleclient_hotspot:0.16.0",
        "io.prometheus:simpleclient_httpserver:0.16.0",
        "javax.annotation:javax.annotation-api:1.3.2",
        "junit:junit:4.13.2",
        "me.dinowernli:java-grpc-prometheus:0.6.0",
        "net.javacrumbs.future-converter:future-converter-java8-guava:1.2.0",
        "net.jcip:jcip-annotations:1.0",
        "org.apache.commons:commons-compress:1.26.1",
        "org.apache.commons:commons-lang3:3.13.0",
        "org.apache.commons:commons-pool2:2.11.1",
        "org.apache.tomcat:annotations-api:6.0.53",
        "org.bouncycastle:bcprov-jdk15on:1.70",
        "org.checkerframework:checker-qual:3.38.0",
        "org.jetbrains:annotations:16.0.2",
        "org.mockito:mockito-core:5.10.0",
        "org.openjdk.jmh:jmh-core:1.37",
        "org.openjdk.jmh:jmh-generator-annprocess:1.37",
        "org.projectlombok:lombok:1.18.30",
        "org.redisson:redisson:3.23.4",
        "org.slf4j:slf4j-simple:2.0.13",
        "org.threeten:threetenbp:1.6.8",
        "org.xerial:sqlite-jdbc:3.45.3.0",
        "org.yaml:snakeyaml:2.2",
        "redis.clients:jedis:5.1.2",
    ],
    fail_if_repin_required = True,  # TO RE-PIN: REPIN=1 bazel run @unpinned_maven//:pin
    generate_compat_repositories = True,
    lock_file = "//:maven_install.json",
    repositories = [
        "https://repo.maven.apache.org/maven2",
    ],
    strict_visibility = True,
)
use_repo(
    maven,
    "maven",
    "unpinned_maven",
)

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

# Server base image
oci.pull(
    # This is a multi-arch image!
    name = "amazon_corretto_java_image_base",
    digest = "sha256:f0e6040a09168500a1e96d02fef42a26176aaec8e0f136afba081366cb98e2f6",  # tag:21 as of today.
    image = "public.ecr.aws/amazoncorretto/amazoncorretto",
    platforms = [
        "linux/amd64",
        "linux/arm64/v8",
    ],
)

# Worker base image
oci.pull(
    name = "ubuntu_lunar",
    digest = "sha256:303223fbebbd1095df7299707ed8b0a51be5c0a7eee78292fc65ee2201c2e138",  # tag: lunar
    image = "index.docker.io/bazelbuild/buildfarm-worker-base",
)
use_repo(
    oci,
    "amazon_corretto_java_image_base",
    "ubuntu_lunar",
)

# https://github.com/bazelbuild/rules_python/pull/713#issuecomment-1885628496
# Satisfy running tests in Docker as root.
bazel_dep(name = "rules_python", version = "0.31.0")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    configure_coverage_tool = False,
    ignore_root_user_error = True,
    python_version = "3.11",
)

build_deps = use_extension("//:extensions.bzl", "build_deps")
use_repo(
    build_deps,
    "bazel",
    "io_grpc_grpc_proto",
    "opentelemetry",
    "skip_sleep",
    "tini",
)

googleapis_switched_rules = use_extension("@com_google_googleapis//:extensions.bzl", "switched_rules")
googleapis_switched_rules.use_languages(
    grpc = True,
    java = True,
)
use_repo(googleapis_switched_rules, "com_google_googleapis_imports")

find_rpm = use_extension("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild_bzlmod")
use_repo(find_rpm, "rules_pkg_rpmbuild")

register_toolchains("@rules_pkg_rpmbuild//:all")
